import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是首页页面</div>;
  }
}

class About extends Component {
  render() {
    return <div>这是关于页面</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>这是我的页面</div>;
  }
}

class Detail extends Component {
  render() {
    // todo 当一个组件通过路由引入的时候，props就是一个对象
    // todo history用来做编程式导航, location用来获取url信息, match用来获取参数
    // todo 统称为路由信息
    // todo ?.叫做可选链操作符
    console.log(this.props?.match?.params?.id);
    // this.props.history.push?.();
    return <div>这是详情页面 - {this.props.match.params.id}</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-params动态路由</h2>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
          <li>
            <Link to="/detail/234">detail</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
          {/* 动态路由 */}
          <Route path="/detail/:id" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
